<!--
 * @Author: your name
 * @Date: 2021-08-26 18:49:33
 * @LastEditTime: 2021-08-28 15:20:26
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_study\src\views\order\CreateOrder.vue
-->
<template>
  <div class="create-order">
    <nav-bar>
      <template v-slot:center> 订单预览 </template>
    </nav-bar>

    <div class="address-warp">
      <div class="name" @click="goTo">
        <span>{{ address.name }}</span>
        <span>{{ address.phone }}</span>
      </div>
      <div class="address">
        {{ address.province }}{{ address.city }}{{ address.county
        }}{{ address.address }}
      </div>
      <van-icon class="arrow" name="arrow" />
    </div>
    <div class="good">
      <div class="good-item" v-for="(item, index) in cartList" :key="index">
        <div class="good-img"><img v-lazy="item.goods.cover_url" alt="" /></div>
        <div class="good-desc">
          <div class="good-title">
            <span>{{ item.goods.title }}</span>
            <span>x{{ item.num }}</span>
          </div>
          <div class="good-btn">
            <div class="price">
              ￥<small>{{ item.goods.price + ".00" }}</small>
            </div>
          </div>
        </div>
      </div>
    </div>

    <van-submit-bar
      class="submit-all"
      :price="total * 100"
      button-text="生成订单"
      @submit="handleCreateOrder"
    >
      商品金额
    </van-submit-bar>

    <van-popup
      :close-on-click-overlay="false"
      closeable
      v-model:show="showPay"
      position="bottom"
      :style="{ height: '40%' }"
      @close="close"
    >
      <van-grid :border="false" :column-num="2">
        <van-grid-item>
          支付宝二维码<br />
          <van-image
            width="150"
            height="150"
            :src="aliyun"
          />
        </van-grid-item>
        <van-grid-item>
          微信二维码<br />
          <van-image
            width="150"
            height="150"
            :src="aliyun"
          />
        </van-grid-item>
      </van-grid>
    </van-popup>
  </div>
</template>

<script>
import { ref, reactive, torefs, onMounted, computed, toRefs } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { Toast } from "vant";
import {
  createOrder,
  getOrderPreview,
  payOrder,
  payOrderStatus,
} from "network/order";
import NavBar from "components/common/navbar/NavBar";
export default {
  components: { NavBar },
  name: "CreateOrder",
  setup() {
    const router = useRouter();
    const route = useRoute();
    const store = useStore();

    const state = reactive({
      cartList: [],
      address: {},
      showPay: false,
      orderNo: "",
      aliyun: "",
      wechat: "",
    });

    const init = () => {
      Toast.loading({ message: "加载中。。。", forbidClick: true });

      getOrderPreview().then((res) => {
        // console.log(res);
        let address = res.address.filter((n) => n.is_default == "1");

        if (address.length == 0) {
          state.address = {
            address: "还没有设置默认地址，请选择地址或新增地址",
          };
        } else {
          state.address = address[0];
        }

        state.cartList = res.carts;
      });
    };

    onMounted(() => {
      init();
    });

    const goTo = () => {
      router.push({ path: "/address" });
    };

    const handleCreateOrder = () => {
      const params = {
        address_id: state.address.id,
      };

      createOrder(params).then((res) => {
        Toast("创建订单成功");
        store.dispatch("updateCart");

        state.showPay = true;
        //获取订单id
        state.orderNo = res.id;

        payOrder(state.orderNo, { type: "aliyun" }).then((res) => {
          state.aliyun = res.qr_code_url;
          // console.log(res);
          // console.log(state.aliyun);
        });
        
        //由于微信难以测试，所有使用阿里云
        payOrder(state.orderNo,{type:'aliyun'}).then(res=>{
            state.aliyun=res.qr_code_url
        })

        //轮询查看
        // const timer=setInterval(() => {
        //     payOrderStatus(state.orderNo).then(res=>{
        //         if(res=='2'){
        //             clearInterval(timer)
        //             router.push({path:'/orderdetail',query:{id:state.orderNo}})
        //         }
        //     })
        // }, 2000);

        setTimeout(() => {
          router.push({path:'/orderdetail',query:{id:state.orderNo}})
        }, 2000);
      });
    };

    const close = () => {
      router.push({path:'/orderdetail',query:{id:state.orderNo}})
    };

    const total = computed(() => {
      let sum = 0;
      state.cartList.forEach((item) => {
        sum += parseInt(item.num) * parseFloat(item.goods.price);
      });
      return sum;
    });

    return {
      ...toRefs(state),
      goTo,
      handleCreateOrder,
      close,
      getOrderPreview,
      total,
    };
  },
};
</script>

<style scoped lang="scss">
.create-order {
  background-color: #fff;
  .address-warp {
    text-align: left;
    margin-bottom: 20px;
    background: #f4f6f6;
    position: relative;
    margin-top: 45px;
    font-size: 14px;
    padding: 15px;
    color: #1c2833;
    .name {
      margin: 10px 0;
      span:nth-child(1) {
        margin-right: 10px;
      }
    }
    .address {
      margin: 10px 0;
    }
    .arrow {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 20px;
    }
    &::before {
      position: absolute;
      right: 0;
      bottom: 0;
      left: 0;
      height: 2px;
      background-size: 80px;
      background: #82e0aa;
      content: "";
    }
  }
  .good {
    margin-bottom: 120px;
  }
  .good-item {
    padding: 10px;
    background: #fff;
    display: flex;
    .good-img {
      img {
        width: 100px;
        height: auto;
      }
    }
    .good-desc {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      padding: 20px;
      .good-title {
        display: flex;
        justify-content: space-between;
      }
      .good-btn {
        display: flex;
        justify-content: space-between;
        .price {
          font-size: 16px;
          color: red;
          line-height: 28px;
        }
        .van-icon-detele {
          font-size: 20px;
          margin-top: 4px;
        }
      }
    }
  }
  .pay-warp {
    position: fixed;
    bottom: 0;
    left: 0;
    widows: 100%;
    background: #fff;
    padding: 10px 0;
    padding-bottom: 50px;
    border-top: 1px solid #e9e9e9;
    > div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 14px;
      span:nth-child(2) {
        color: red;
        font-size: 18px;
      }
    }
    .pay-btn {
      position: fixed;
      bottom: 7px;
      right: 0;
      left: 0;
      width: 90%;
      margin: 0 auto;
    }
  }
}
.submit-all {
  margin-bottom: 50px;
  z-index: 9 !important;
}
</style>